﻿using System;
using System.Collections.Generic;
using System.IO ;
using System.Text;

namespace HttpModuleAccessControl
{
    class Log
    {
        private static Object lock1 = new Object();

        private static bool _ifLog = Config.ifLog;

        /// <summary>
        /// Write message to log file
        /// </summary>
        /// <param name="Message"></param>
        public static void Write(string Message)
        {
            lock (lock1)
            {
                if (_ifLog)
                {
                    // you can design your own log mechanism or use NLog.

                    // future : path from a config file
                    // future : consider NLog or other campatitive component
                    using (StreamWriter sw = new StreamWriter("c:\\log.txt", true))
                    {
                        sw.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + Message);
                    }
                }
 
            }
        }

        public static void DumpRBAC(Users users, Roles roles, Permissions permissions)
        {
            DumpUsers(users);
            DumpRoles(roles);
            DumpPermissions(permissions);
        }

        public static void DumpUsers(Users users)
        {
            Log.Write("Dumping User information..");
            foreach (User u in users)
            {
                Log.Write("UserID: " + u.UserID + ", Name: " + u.Name + ", Title: " + u.UserTitle);
                foreach (object o1 in u.RoleList)
                {
                    Log.Write("with roleID= " + o1.ToString());
                }
            }
        }

        public static void DumpRoles(Roles roles)
        {
            Log.Write("Dumping Role information..");
            foreach (Role r in roles)
            {
                Log.Write("Role :" + r.RoleID + "," + r.RoleName);
                foreach (object o1 in r.PermissionList)
                {
                    Log.Write("with permissionID= " + o1.ToString());
                }
            }
        }

        public static void DumpPermissions(Permissions permissions)
        {
            Log.Write("Dumping Permission information..");
            foreach (Permission p in permissions)
            {
                Log.Write("Permission :" + p.PermissionID + "," + p.PermissionName);
            }
        }
    }
}
